package com.hp.demo.vteam.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hp.demo.vteam.bean.AccountGroup;

public class AccountGroupServlet extends BaseServlet {
	private static final long serialVersionUID = 8801298924461310653L;

	@Override
	public void edit (HttpServletRequest request, HttpServletResponse response)
    throws Exception { 

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String desc = request.getParameter("desc");
        Date date = new Date();
        AccountGroup accountGroup = null;
        if(id!=null && !id.isEmpty()){
        	accountGroup = pm.getObjectById(AccountGroup.class, Long.parseLong(id));
        	accountGroup.setName(name);
        	accountGroup.setDesc(desc);
        	accountGroup.setAddedTime(date);
        	accountGroup.setAddedBy(user);
        }
       	if(accountGroup == null){
       		accountGroup = new AccountGroup(name,desc, user, date);    
        }
        super.baseEdit(request, response, accountGroup);
		response.getWriter().print(String.format(SUCCESS_MSG, "true","操作成功" ));
	}
	
	@Override
	public void del (HttpServletRequest request, HttpServletResponse response)
    throws Exception {

        String id = request.getParameter("id");
        AccountGroup accountGroup = null;

        if(id!=null && !id.isEmpty()){
        	accountGroup = pm.getObjectById(AccountGroup.class, Long.parseLong(id));
        }
        System.out.println(accountGroup);
        super.baseDel(request, response, accountGroup);
		response.getWriter().print(String.format(SUCCESS_MSG, "true","操作成功" ));
		response.getWriter().print("{success: true}");
	}

	@Override
	public void search (HttpServletRequest request, HttpServletResponse response)
    throws Exception {

//		for(int i = 10;i < 50; i ++){
//			pm.makePersistent(new AccountGroup("AccountGroupName " + i," Description " + i,new User("robin.du1982@gmai.com","authDomain"),new Date()));
//		}
		//System.out.println("=====I'm in " + this.getClass() + "'s search method=====");
		super.baseSearch(request, response, AccountGroup.class);
	}
}

